package com.gcsoftware.utils;

import java.util.Random;

/**
 * Created by qs on 2018/3/23.
 */
public class KeysUtils {
    private static final String CONTAINS_LOWERCASE_REGEX= ".*[a-z]{1,}.*";
    private static final String CONTAINS_UPPERCASE_REGEX= ".*[A-Z]{1,}.*";
    private static final String CONTAINS_NUMBER_REGEX= ".*\\d{1,}.*";
    private static final String CONTAINS_SPECIALCODE_REGEX= ".*[~!@#$%^&*\\.?]{1,}.*";

    //10位以上，包含大小写英文字母，数字和特殊字符组成
    public static String generatePlaintextKey(int len) {
        String result = makeRandomPassword(len);
        if (result.matches(CONTAINS_LOWERCASE_REGEX) && result.matches(CONTAINS_UPPERCASE_REGEX) && result.matches(CONTAINS_NUMBER_REGEX) && result.matches(CONTAINS_SPECIALCODE_REGEX)) {
            return result;
        }
        return generatePlaintextKey(len);
    }

    //随机密码生成
    public static String makeRandomPassword(int len){
        char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int x = 0; x < len; ++x) {
            sb.append(charr[r.nextInt(charr.length)]);
        }
        return sb.toString();
    }

}
